<?php
/**
 * Permission Service
 * User: zng
 * Date: 2019/11/4
 * Time: 14:34
 */
namespace app\admin\service\tools;

use app\admin\model\tools\AccountCategory as AccountCategoryModels;
use think\db\Query;

Class AccountCategory {

    /**
     * 首页列表
     * @param array $get 筛选条件
     * @author zx
     * @date 2019-12-06
     * @return \think\Paginator
     */
    public function indexPaginate( array $get=[] )
    {
        $map = [];
        if ( get($get, 'title') ) {
            $map[] = ['title', 'LIKE', $get['title'].'%'];
        }
        if ( get($get, 'status') === 0 || get($get, 'status') === '0' || get($get, 'status') == 1 ) {
            $map[] = ['status', '=', $get['status']];
        }
        $pageSize = get($get,'pageSize')>0?$get['pageSize']:20;
        return AccountCategoryModels::where($map)
            ->order('id', 'desc')
            ->paginate($pageSize);
    }

    /**
     * 状态更改
     * @param AccountCategoryModels $model
     * @author zx
     * @date 2020-03-17
     * @return bool
     */
    public function status(AccountCategoryModels $model)
    {
        $status = (int)(!$model->status);
        $model->status = $status;
        return $model->save();
    }

    /**
     * 新增\编辑信息
     * @param array $data 数据包
     * @author zx
     * @date 2020-03-17
     * @return boolean|integer
     */
    public function create(array $data)
    {
        $accountCategory = AccountCategoryModels::create($data, ['title','status']);
        return $accountCategory->isEmpty()?false:$accountCategory->id;
    }

    /**
     * 批量删除
     * @param mixed $data
     * @author zx
     * @date 2020-03-17
     * @return boolean
     */
    public function destory( $data )
    {
        return AccountCategoryModels::destroy(function(Query $query)use($data){
            $where = ['id', '=', $data];
            if ( is_array($data) ) {
                $where = ['id', 'in', $data];
            }
            $query->where([
                $where
            ]);
        });
    }

    /**
     * 菜品分类编辑
     * @param AccountCategoryModels $model
     * @param array $data
     */
    public function save(AccountCategoryModels $model, array $data)
    {
        $model->title = $data['title'];
        $model->status = $data['status'];
        return $model->save();
    }

    /**
     * 获取菜品分类列表
     */
    public function lists()
    {
        return AccountCategoryModels::field('id,title')->status()->select();
    }

}